
import moment from 'moment';
import axios from "axios";
class QmtSource {
    constructor(ready) {
        if(ready) {
            ready();
        }
    }

    async geKLines(code, period) {
        console.log("get lines", code, period);
        if(period == "1min"){
            period = "1m"
        }
        if(period == "15min"){
            period = "15m"
        } else if(period == "30min"){
            period = "30m"
        } else if(period == "1h"){
            period = "1h"
        } else if(period == "1w") {
            period = "1w"
        } else if(period == "1M") {
            period = "1M"
        } else if(period == "1d") {
            period = "1d"
        }

        let url = "https://stock.deployment.cn/api/call"
//call("get_market_data_ex", [[], ["000001.SZ"]], {"period": "1d", "start_time":"20250626", "end_time":"20250627"})
        let params = {
            func: "get_market_data_ex",
            args: [[], [code]],
            kwargs: {"period": period, "count": 500, "end_time": moment().format("YYYYMMDDHHmmss")}
        };

        try {
            const res = await axios.post(url, params, {
                headers: { "Content-Type": "application/json" }
            });
            if (res.data) {
                return res.data[code].map(item => ({
                    time: item.time,
                    timestamp: item.time,
                    open: parseFloat(item.open),
                    high: parseFloat(item.high),
                    low: parseFloat(item.low),
                    close: parseFloat(item.close),
                    volume: parseFloat(item.volume),
                    amount: parseFloat(item.amount)
                }));
            } else {
                throw new Error(res.data.reason);
            }
        } catch (err) {
            console.error(`❌ 请求失败 ${code} ${period} - ${err.message}`);
            return [];
        }

    }
}
export default QmtSource;
